<template>
	<view>
		<navBar title="我的地址" backJudge></navBar>
		<view class="addressList" v-if="list.length>0">
			<view class="item" @click="toDetail(item)" v-for="(item,index) in list" :key="index">
				<view style="display: flex;align-items: center;">
					<view class="default" v-if="item.isDefault.key === 1">默认</view>
					<view class="where" v-if="item.city">{{item.city.mergerName}}</view>
				</view>
				<view style="display: flex;align-items: center;flex-wrap: wrap;">
					<uni-icons class="icon" type="location-filled"></uni-icons>
					<view class="detail">{{item.detail}}</view>
					
				</view>
				<view  style="display: flex;align-items: center;justify-content: space-between;" >
					<view class="name">{{item.userName}} {{item.phone}}</view>
					<view v-if="type" @click.stop="goeditaddress(item)" style="width: 70rpx;height: 50rpx;font-size: 22rpx;text-align: center;line-height: 50rpx;color: #fff;background-color: red;border-radius: 10rpx;">编辑</view>
				  <view v-if="!type" @click.stop="delet(item.id)" style="width: 70rpx;height: 50rpx;font-size: 22rpx;text-align: center;line-height: 50rpx;color: #fff;background-color: red;border-radius: 10rpx;">删除</view>
				</view>
			</view>
		</view>
		<view v-else style="display: flex;flex-direction: column;align-items: center;">
			<image style="width: 250rpx;height: 250rpx;margin-top: 200rpx;" src="../../static/address.png" mode=""></image>
			<text style="font-size: 40rpx;margin-top: 40rpx;font-weight: bold;">暂无收货地址~</text>
		</view>
		<view class="buttonBox" @click="toAdd(item)">添加地址</view>
	</view>
</template>

<script>
	import {address,deletaddress} from '@/utils/API.js'
	import navBar from '../../components/navbars/navbars.vue'
	export default {
		data() {
			return {
				list:[
					{
						default:true,
						where:'北京北京市朝阳区奥运村街道',
						detail:'四附院一栋1564',
						name:'张 (先生) 18794566158'
					}
				],
				type:''
			}
		},
		components:{
			navBar
		},
		onLoad(e) {
			this.type = e.type
			this.getData()
		},
		methods: {
			
			async delet(e){
				var data = {id:e}
				const result = await deletaddress(data)
				if(result.code==200){
					this.getData()
				}
				
			},
			async getData(){
				const result = await address()
				if(result.code==200){
					this.list = result.data
				}
			
			},
			toAdd(){
				uni.navigateTo({
					url:'/pages-user/addDetail/addDetail?type=add'
				})
			},
			goeditaddress(item){
				uni.navigateTo({
					url:'/pages-user/addDetail/addDetail?id='+item.id
				})
			},
			toDetail(item){
				if(this.type){
					uni.$emit('address',item)
					uni.navigateBack()
					return false
				}
				uni.navigateTo({
					url:'/pages-user/addDetail/addDetail?id='+item.id
				})
			}
		},
		onShow() {
			this.getData()
		},
		
	}
</script>

<style lang="scss">
	page{
		background-color: $MainBackColor;
	}
</style>

<style lang="scss" scoped>
	.buttonBox{
		position: fixed;
		bottom:0px;
		left:0px;
		background-color: $blue;
		color: white;
		padding: 10px;
		width: calc(100vw - 60px);
		margin: 20px;
		text-align: center;
		border-radius: 20px;
	}
	
	.addressList{
		padding-top: 40rpx;
		.item{
			width: 710rpx;
			margin: 0 auto;
			box-sizing: border-box;
			background-color: white;
			padding: 10px 20px;
			border-radius: 10px;
			margin-bottom: 10px;
			.default{
				color: #FA5151;
				background-color: rgba(250, 81, 81, 0.10);
				border: 1px solid #FA5151;
				font-size: 12px;
				padding: 2px;
				margin-right: 10px;
			}
			.where{
				font-size: 14px;
			}
			.name{
				font-size: 12px;
				color: $grey;
			}
			.detail{
				font-weight: bold;
				font-size: 14px;
				margin: 5px 0px;
			}
		}
	}

</style>
